package res;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class Settings {
  private Properties prop;
  private File propFile;
  
  private static final String PROPERTY_DESCRIPTION = "This file belongs to the thps-qb parser.";
  private static final String PROPERTY_FILE = "THQBEditor.properties";
  
  private static final String LAST_FOLDER_OPENED = "last.folder";
  
  private static Settings instance = null;
  
  public static Settings getInstance(){
    if(instance == null){
      instance = new Settings();
    }
    return instance;
  }
  
  private Settings(){
    prop = new Properties();
    propFile = new File(PROPERTY_FILE);
    if(propFile.exists())
      load();
  }
  
  public File getFolder(){
    String path = prop.getProperty(LAST_FOLDER_OPENED);
    if(path == null)
      return null;
    return new File(path);
  }
  
  public void saveFolder(File file){
    prop.setProperty(LAST_FOLDER_OPENED, file.getPath());
    save();
  }
  
  private void save(){
    try {
      FileOutputStream os = new FileOutputStream(propFile);
      prop.store(os, PROPERTY_DESCRIPTION);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    
  }
  
  private void load(){
    FileInputStream is;
    try {
      is = new FileInputStream(propFile);
      prop.load(is);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
